//home模块小仓库
import { reqMyOrderList,reqAddCollect,reqMyCollectList,reqDeleteCollect,reqCartToCollect,reqDeleteOrder,reqMyHistoryOrderList} from "@/api";
const state = {
    orderList:{},
    historyOrderList:{},
    collectList:{},
};
const mutations = {
    GETORDERLIST(state, orderList) {
        state.orderList = orderList;
    },
    GETHISTORYORDERLIST(state, historyOrderList) {
        state.historyOrderList = historyOrderList;
    },
    GETCOLLECTLIST(state, collectList){
        state.collectList=collectList;
    }
};
const actions = {
    //获取订单数据
    async getOrderList({ commit },{page,limit}) {
        let result = await reqMyOrderList(page,limit);
        if (result.code == 200) {
            commit("GETORDERLIST", result.data)
        }
    },
    //获取历史记录数据
    async getHistoryOrderList({ commit },{page,limit}) {
        let result = await reqMyHistoryOrderList(page,limit);
        if (result.code == 200) {
            commit("GETHISTORYORDERLIST", result.data)
        }
    },
    //取消订单
    async deleteOrder({commit},orderId){
        let result = await reqDeleteOrder(orderId);
        if(result.code==200){
            return 'ok';
        }else{
            //当前函数如果执行返回Promise
            return Promise.reject(new Error('false'));  
        }
    },
    //增加一个商品到收藏
    async addCollect({commit},{waresId}){
        let result=await reqAddCollect(waresId);
        if(result.code==200){
            return 'ok';
        }else{
            //当前函数如果执行返回Promise
            return Promise.reject(new Error('false'));  
        }
    },
    //获取收藏数据
    async getCollectList({ commit },{page,limit}) {
        let result = await reqMyCollectList(page,limit);
        if (result.code == 200) {
            commit("GETCOLLECTLIST", result.data)
        }
    },
    //删除收藏中商品
    async deleteCollectById({ commit }, collectId) {
        let result = await reqDeleteCollect(collectId);
        if (result.code == 200) {
            return 'ok';
        } else {
            return Promise.reject(new Error('faile'));
        }
    },
    //提交购物车商品到收藏
    async insertCartToCollect({ commit }) {
        let result = await reqCartToCollect();
        if (result.code == 200) {
            return 'ok';
        } else {
            return Promise.reject(new Error('faile'));
        }
    },


};
const getters = {};
export default {
    state,
    mutations,
    actions,
    getters
}